/*
 * Copyright (C) 2009 CityLights Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.schizoidman.android.citylights;

import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.webkit.*;
import android.util.Log;
import android.net.Uri;

/**
 * Created by IntelliJ IDEA.
 * User: xfeldman
 * Date: Sep 2, 2009
 * Time: 3:19:43 AM
 * To change this template use File | Settings | File Templates.
 */
public class WebView extends Activity {

//    private static final String LOG_TAG = "WebViewDemo";

    private android.webkit.WebView mWebView;

    private Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.webview);
        mWebView = (android.webkit.WebView) findViewById(R.id.webview);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);

        mWebView.setWebChromeClient(new MyWebChromeClient());
        mWebView.setWebViewClient(new MyWebViewClient());

//        mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");

        Log.d("CityLights", "WebView URL: " + getIntent().getDataString());
        Uri webUri = Uri.parse("http://" +
                getIntent().getData().getAuthority().concat(
                        "/viewType.html?buildTypeId="+getIntent().getStringExtra("build.id")+"&tab=buildTypeStatusDiv"));

        mWebView.loadUrl(webUri.toString());//getIntent().getDataString());
    }

    final class DemoJavaScriptInterface {

        DemoJavaScriptInterface() {
        }

        /**
         * This is not called on the UI thread. Post a runnable to invoke
         * loadUrl on the UI thread.
         */
        public void clickOnAndroid() {
            mHandler.post(new Runnable() {
                public void run() {
                    mWebView.loadUrl("javascript:wave()");
                }
            });

        }
    }

    /**
     * Provides a hook for calling "alert" from javascript. Useful for
     * debugging your javascript.
     */
    final class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(android.webkit.WebView view, String url, String message, JsResult result) {
  //          Log.d(LOG_TAG, message);
            result.confirm();
            return true;
        }
    }


    private class MyWebViewClient extends WebViewClient {
        @Override
        public void onReceivedHttpAuthRequest(android.webkit.WebView webView, HttpAuthHandler httpAuthHandler, String host, String realm) {
            Log.d("CityLights", "HTTP Authentication challenge is received");
            httpAuthHandler.proceed("mfeldman", "forcefeedbAck");
        }
    }
}


